package control;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.gallery.GalleryService;
import model.gallery.GalleryVO;
import model.member.MemberVO;
import model.util.ListVO;
import model.util.ReplyVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class GalleryController extends MultiActionController {
	private GalleryService galleryService;

	public GalleryController(GalleryService galleryService) {
		super();
		this.galleryService = galleryService;
	}
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String page = request.getParameter("page");
		String viewName = "home";
		ModelAndView mv = new ModelAndView();
		if(!page.equals("home")){
			viewName = page+".gallery";
		}
		if(page.equals("update")){
			int no = Integer.parseInt(request.getParameter("no"));
			int uid = Integer.parseInt(request.getParameter("uid"));
			mv.addObject("gvo", galleryService.content(no,uid));
		}
		mv.setViewName(viewName);
		return mv;
	}
	public ModelAndView write(HttpServletRequest request,
			HttpServletResponse response, GalleryVO vo) throws Exception {
		ModelAndView mv = new ModelAndView();
		//System.out.println(vo);
		String path = "index";
		try {
			HttpSession session = request.getSession(false);
			if(session != null && session.getAttribute("mvo")!=null){
				MemberVO memberVO = (MemberVO) session.getAttribute("mvo");
				vo.setMemberVO(memberVO);
				galleryService.write(vo);
				path = "redirect:member.do?mode=memberMain&uid="+memberVO.getuId()+"&page=1";
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}	
		mv.setViewName(path);
		return mv;
	}

	public ModelAndView content(HttpServletRequest request,
			HttpServletResponse response, GalleryVO vo) throws SQLException {
		HashMap map=null;
		try {
			map = galleryService.content(vo.getNo(),vo.getMemberVO().getuId());
			ArrayList<ReplyVO> list = galleryService.reply_view(vo.getNo());
			map.put("list", list);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("content.gallery","cvo",map);		
	}
	public ModelAndView listByUser(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int uid = Integer.parseInt(request.getParameter("uid"));
		int page = Integer.parseInt(request.getParameter("page"));
		
		return new ModelAndView("JsonView","list",galleryService.listByUser(uid, page));
	}
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws SQLException{
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		int no = Integer.parseInt(request.getParameter("no"));
		String orgImg = request.getParameter("img");
		String thumbImg = request.getParameter("thumb");
		galleryService.delete(no,orgImg,thumbImg);
		return new ModelAndView("redirect:member.do?mode=memberMain&uid="+mvo.getuId()+"&page=1");
	}
	public ModelAndView update(HttpServletRequest request, HttpServletResponse response, GalleryVO gvo) throws SQLException{
		MemberVO mvo = (MemberVO)request.getSession().getAttribute("mvo");
		galleryService.update(gvo);
		return new ModelAndView("redirect:member.do?mode=memberMain&uid="+mvo.getuId()+"&page=1");
	}
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String category = request.getParameter("category");
		String page = request.getParameter("page");
		return new ModelAndView("list.gallery","gvo",galleryService.list(category, page));
	}
	
public ModelAndView reply_write(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		
		String no = request.getParameter("gallery_no");
		int gallery_no = Integer.parseInt(no);
		String m_uid = request.getParameter("m_uid");
		String content = request.getParameter("content");
		String re = request.getParameter("restep");
		
		int restep = 0;
		
		if(re.equals("undefined")){
			restep=0;
		}else if(restep>=0){
			restep=galleryService.getRestep(gallery_no)+1;
		}
		galleryService.reply_write(gallery_no,m_uid,content,restep);
		
		ArrayList<ReplyVO> list = galleryService.reply_view(gallery_no);
		return new ModelAndView("JsonView","list",list);
	}
	
	public ModelAndView reply_delete (HttpServletRequest request,HttpServletResponse response,ReplyVO vo) throws SQLException{
		ModelAndView mv = new ModelAndView();
		int uId = Integer.parseInt(request.getParameter("uId"));
		galleryService.reply_delte(vo);
		String path = "redirect:gallery.do?mode=content&no="+vo.getRef()+"&memberVO.uId="+uId;
		mv.setViewName(path);
		return mv;	
	}
	
	//--------------		추천관련 시작		--------------> 
	public ModelAndView wow(HttpServletRequest request,HttpServletResponse response,GalleryVO vo){
		ModelAndView mv=new ModelAndView();
		String path = "index";
		HttpSession session = request.getSession(false);
		if(session != null && session.getAttribute("mvo")!=null){
			MemberVO memberVO = (MemberVO) session.getAttribute("mvo");
			vo.setMemberVO(memberVO);
			try {
				galleryService.wow_update(vo);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			path = "redirect:gallery.do?mode=content&no="+vo.getNo()+"&memberVO.uId="+memberVO.getuId();
		}	
		mv.setViewName(path);
		return mv;
	}
	//--------------		추천관련 끝		-------------->
	
	public ModelAndView list_best(HttpServletRequest request,HttpServletResponse response,GalleryVO vo) throws SQLException{
		HashMap listBmap= galleryService.list_best();	
		//System.out.println(listBmap.get("pList"));
		return new ModelAndView("list_best.gallery","listBmap",listBmap);
		
	}
	public ModelAndView list_main(HttpServletRequest request,HttpServletResponse response,GalleryVO vo) throws SQLException{
		HashMap listMap= galleryService.list_best();	
		//System.out.println(listBmap.get("pList"));
		return new ModelAndView("list_main.gallery","listMap",listMap);
	}
	/*public ModelAndView best_register(HttpServletRequest request,HttpServletResponse reponse,GalleryVO vo) throws SQLException{
		galleryService.best_register(vo);
		return new ModelAndView();
		
	}*/
	
	public ModelAndView best_weekly(HttpServletRequest request,HttpServletResponse reponse,GalleryVO vo) throws SQLException{
		ArrayList wList=new ArrayList();
		String page=request.getParameter("page");
		
		ListVO lvo = galleryService.getBest_weekly(page);
		
		return new ModelAndView("best_weekly.gallery","lvo",lvo);
		
	}

	public ModelAndView weekly(HttpServletRequest request,HttpServletResponse reponse,GalleryVO vo) throws SQLException{
		int  bestPrimary= Integer.parseInt(request.getParameter("best"));
		System.out.println(bestPrimary);
		HashMap listMap=galleryService.weekly(bestPrimary);
			 
		return new ModelAndView("gallery/best_content","listMap",listMap);

	}
	
}
